package com.springboot.config.security.admin;

import com.springboot.entity.Administrator;
import com.springboot.enums.ErrorEnum;
import com.springboot.exception.MyException;
import com.springboot.repository.AdministratorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;


/**
 *@author: lzh
 */
@Component
public class AdministratorDetailsService implements UserDetailsService {

    @Autowired
    AdministratorRepository administratorRepository;

    @Override
    public UserDetails loadUserByUsername(String username){
        Administrator administrator = administratorRepository.findByUsername(username);
        if(administrator == null) {
            throw new MyException(ErrorEnum.ADMINISTRATOR_NOT_EXIST);
        }
        AdministratorDetails administratorDetails = new AdministratorDetails(administrator);
        return administratorDetails;
    }
}
